package com.yanqu.road.server.cross3server.cmd.xrank;

import com.yanqu.road.logic.pb.XRankPb;
import com.yanqu.road.logic.xlogic.rank.channel.XChannelRankListModel;
import com.yanqu.road.logic.xlogic.rank.channel.XChannelRankMap;
import com.yanqu.road.pb.cross3.activity.XRankProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.cross3server.manager.Cross3XRankMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.Cross3Protocol;
import com.yanqu.road.utils.ConfigHelper;

@Cmd(code = Cross3Protocol.C3_XRANK_GET_CHANNEL_RANK_LIST, desc = "区域榜单")
public class XRankGetChannelRankListFromGameCmd extends CrossCommand {

    private int activityId;
    private int conditionType;

    private long userId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        XRankProto.GetXChannelRankListReqMsg reqMsg = XRankProto.GetXChannelRankListReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        conditionType = reqMsg.getConditionType();

        userId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        XChannelRankMap channelRankMap = Cross3XRankMgr.getChannelRankMap();
        XChannelRankListModel rankListModel = channelRankMap.getChannelRankListModel(activityId, conditionType);

        XRankProto.XChannelRankListMsg.Builder rankListMsg = XRankPb.parseXChannelRankListMsg(activityId, conditionType, rankListModel.getRankList());
        int myRank = rankListModel.getMyRank(ConfigHelper.getInt("channelId"));

        XRankProto.GetXChannelRankListRespMsg.Builder builder = XRankProto.GetXChannelRankListRespMsg.newBuilder();
        builder.setRet(0);
        XRankPb.fillFlagId(rankListMsg);
        builder.setRankList(rankListMsg);
        builder.setMyRank(myRank);
        YanQuMessage message = YanQuMessageUtils.buildMessage(ClientProtocol.U_XRANK_GET_CHANNEL_RANK_LIST, builder);
        MessageHelper.sendPacket(serverId, userId, message);
    }

    @Override
    public void sendData() {

    }
}
